const enterpriseVue = new Vue({
  el: '#app',
  data() {
    return {
      pickerOptions: pickerOptions,
      rules: {
        Sales_Order_SerialNumber: [{ required: true, message: '请输入销售订单号', trigger: 'blur' }],
        Store_SerialNumber: [{ required: true, message: '请输入门店编号', trigger: 'blur' }],
        Sales_Date: [{ required: true, message: '请输入销售日期', trigger: 'blur' }],
        Open_ID: [{ required: true, message: '请输入消费人员编号', trigger: 'blur' }],
        Sales_Amount: [{ required: true, message: '请输入消费总额', trigger: 'blur' }],
        Collection_Amount: [{ required: true, message: '请输入实收金额', trigger: 'blur' }],
        Sales_ID: [{ required: true, message: '请输入销售员ID', trigger: 'blur' }],

      },
      form: {
        Sales_Order_SerialNumber: '',
        Store_SerialNumber: '',
        Sales_Date: '',
        Open_ID: '',
        Sales_Amount: '',
        Collection_Amount: '',
        Sales_ID: '',
        Row_Status: '',
        Operation_ID: '',
        Operation_Datetime: '',
        Enterprise_Info_SerialNumber: $.cookie("enterprise"),
        Children: {
          sales_order_detail: [{       //采购订单明细
            Sales_Order_Detail_SerialNumber: '',
            Sales_Order_SerialNumber: '',
            Article_SerialNumber: '',
            Quantity: '',
            Unit_Price: '',
            Sales_Unit_Price: '',
            Amount: '',
            Row_Status: '',
            Operation_ID: '',
            Operation_Datetime: '',
          }],
        }
      },
      loading: false
    }
  },
  mounted() {
    const Sales_Order_SerialNumber = sessionStorage.getItem("Sales_Order_SerialNumber")
    this.getData(Sales_Order_SerialNumber)
  },
  methods: {
    getData(Sales_Order_SerialNumber) {
      let _this = this
      $.ajax({
        url: DomainName + '/SalesOrder_Edit',
        type: 'POST',
        data: { Data: Sales_Order_SerialNumber, token: $.cookie("token") },
        async: false,
        success: function (res) {
          console.log(res);
          _this.form = res.sales_order[0]
          if (!_this.form.Children.sales_order_detail || _this.form.Children.sales_order_detail.length == 0) {
            _this.form.Children.sales_order_detail = [{
              Sales_Order_Detail_SerialNumber: '',
              Sales_Order_SerialNumber: _this.form.Sales_Order_SerialNumber,
              Article_SerialNumber: '',
              Quantity: 1,
              Unit_Price: '',
              Sales_Unit_Price: '',
              Amount: '',
              Row_Status: '',
              Operation_ID: '',
              Operation_Datetime: '',
            }]
          }
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    submitForm(formName) {
      let _this = this
      if (_this.form.Children.Sales_Order_Detail[0].Sales_Order_Detail_SerialNumber == '' || _this.form.Children.Sales_Order_Detail[0].Article_SerialNumber == '' || _this.form.Children.Sales_Order_Detail[0].Sign == '' || form.Children.sales_order_detail[0].Quantity == '' || form.Children.sales_order_detail[0].Unit_Price == '' || form.Children.sales_order_detail[0].Sales_Unit_Price == '' || form.Children.sales_order_detail[0].Amount == '') {
        return _this.$message.error("请完善信息")
      }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.loading = true
          $.ajax({
            url: '../addGeneralBusinessAction',
            type: 'POST',
            data: {
              Data: JSON.stringify({
                enterprise_info: [_this.form]
              }),
              token: $.cookie("token")
            },
            success: function (res) {
              if (res.Status === 'success') {
                _this.$message.success("提交成功")
                window.location.href = './OrderDelivery.html'
              } else {
                _this.$message.error("提交失败，请稍后再试")
              }
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试")
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }

      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    add_barcode() {
      this.form.Children.barcode.push({       //条形码
        BarCode: "",
        Article_SerialNumber: '',
        Row_Status: '',
        Operation_ID: '',
        Operation_Datetime: '',
      })
      console.log(this.form.Children.barcode);
    },
    remove_barcode() {
      if (this.form.Children.barcode.length > 1) {
        this.form.Children.barcode.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    add_purchase_unitprice() {
      this.form.Children.purchase_unitprice.push({     //采购价格单
        Purchase_unitprice_SerialNumber: "",
        Start_Date: '',
        End_Date: '',
        Supplier_SerialNumber: '',
        Row_Status: '',
        Operation_ID: '',
        Operation_Datetime: '',
      })
    },
    remove_purchase_unitprice() {
      if (this.form.Children.purchase_unitprice.length > 1) {
        this.form.Children.purchase_unitprice.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    add_series() {
      this.form.Children.series.push({     //商品类型
        Series_SerialNumber: "",
        Series_Name: '',
        Row_Status: '',
        Operation_ID: '',
        Operation_Datetime: '',
      })
    },
    remove_series() {
      if (this.form.Children.series.length > 1) {
        this.form.Children.series.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
  }
})